<?php
/**
 *
 *----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by PhpStorm.
 * User: huangxh
 * Date: 2018/1/5 0005
 * Time: 下午 12:00
 */

use models\Traits\SendTrait;
use Yaf\Session;
use Yaf\Registry;
use \models\DAO\UserModel;
use ErrorMsg\AbstractErrorMsg;
use ErrorMsg\admin\ErrorTips;

class LoginController extends BaseController
{
    use models\Traits\UserTrait,SendTrait;
    public function indexAction(){
        $this->display('index');
    }

    /**
     * 登陆
     * @throws \Yaf\Exception
     */
    public function doLoginAction(){
        $mobile = empty(Registry::get("parameters")['mobile'])?'':Registry::get("parameters")['mobile'];
        $password = empty(Registry::get("parameters")['password'])?'':Registry::get("parameters")['password'];
        $code = empty(Registry::get("parameters")['code'])?'':Registry::get("parameters")['code'];


        $verify = new \Common\Verify();
        if (!$verify->check($code)) {
            return $this->Json([004,['message'=>'验证码错误']]);
        }
        if (empty($mobile)|| empty($password)){
            return $this->Json([001,['message' =>'用户名、密码不能为空']]);
        }



        $user = UserModel::getInstance();
        $condition = ['mobile' =>$mobile];

        $userResult = $user->findOneBy($condition);
        if (empty($userResult->_id )){
            return $this->Json([002,['message' =>'用户名错误']]);
        }

        $vilidataResult = $this->vilidataUser($userResult->_password,$password);

        if ($vilidataResult){
            //验证密码通过、写入session
            $group = \models\DAO\GroupModel::getInstance();
            $result = $group->findOneBy(['id'=>$userResult->_role],'title');
            $user->where('id = '.$userResult->_id)->setInc('login_count');

            $auth = [
                'id' => $userResult->_id,
                'role' => $userResult->_role,
                'rolename' => $result->_title,
                'username' => $userResult->_username
            ];

            Session::getInstance()->set(session_id(),$auth);

            return $this->Json([200,['message' =>ErrorTips::USER_LOGIN_SUCCESS,'url' => '/admin/index/index']]);
        }else{
            throw new Exception(AbstractErrorMsg::VERIFY_PWD_ERROR, AbstractErrorMsg::BASE_MODEL_EMPTY_TABLE_NAME_NO);
        }
    }

    /**
     * 生成验证码
     */
    public function verifyAction(){

        $verify = new \Common\Verify();
        $verify->imageH = 32;
        $verify->imageW = 100;
        $verify->length = 4;
        $verify->useNoise = false;
        $verify->fontSize = 14;
        return $verify->entry();
    }

    public function logoutAction()
    {
        Session::getInstance()->set(session_id(),null);
        $this->cacheClear();
        $this->redirect('/admin/login/index');
    }

}
